<script setup lang="ts">
import { computed, defineEmits, defineProps, ref } from 'vue'
import { NButton, NInput, NSelect } from 'naive-ui'
import type { IPdfMeta } from '@/views/pdf/DataModel.ts'

// 定义接收的 props
const props = defineProps<{
  pdfMetaList: IPdfMeta[] | null
  isUserSelectSelf: boolean
}>()

// 定义 emit
const emit = defineEmits(['search'])
// 处理搜索相关的数据
const searchQuery = ref('')
// 处理搜索相关的数据
const selectTags = ref<{ label: string; value: string }[]>([])

// 构建标签选项
const tagOptions = computed(() => {
  return props.pdfMetaList ? Array.from(new Set(props.pdfMetaList.flatMap(meta => meta.tags))).map(tag => ({ label: tag, value: tag })) : []
})

const handleSearch = () => {
  console.log('selectedTags', selectTags.value)
  emit('search', { searchQuery: searchQuery.value, selectTags: selectTags.value })
}

const handleChangeSelectTags = (value: any) => {
  selectTags.value = value
}
</script>

<template>
  <div class="pdf-search-component">
    <!-- 用于搜索 PDF 的按钮 -->
    <NButton strong secondary type="primary" class="custom-button" @click="handleSearch">
      搜索 PDF
    </NButton>
    <!-- 用于输入搜索查询的输入框 -->
    <NInput v-model:value="searchQuery" placeholder="输入搜索关键词" />
    <!-- 标签选择 -->
    <NSelect v-model="selectTags" :options="tagOptions" style="width:20%" multiple filterable placeholder="请选择筛选标签" @update:value="handleChangeSelectTags" />
  </div>
</template>

<style scoped>
.pdf-search-component {
  display: flex;
  align-items: center;
}

.pdf-search-component .n-input {
  margin-right: 10px;
}

/* 自定义按钮样式 */
.pdf-search-component .n-button.custom-button {
  padding: 8px 16px;
  cursor: pointer;
}

/* 论文列表样式 */
.paper-list {
  margin-top: 20px;
}

.paper-item {
  background-color: #f0f0f0;
  padding: 10px;
  margin-bottom: 5px;
  border-radius: 5px;
}
</style>
